package com.whut.whw.main;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;

import com.whut.whw.R;
import com.whut.whw.base.BaseActivity;
import com.whut.whw.find.FindFragment;
import com.whut.whw.login.LoginActivity;
import com.whut.whw.message.MsgFragment;
import com.whut.whw.personalinfo.PersonalInfoFragment;
import com.whut.whw.post.PostGoodActivity;
import com.whut.whw.topic.TopicFragment;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;

public class MainActivity2 extends BaseActivity {
    public static final int FIND = 0;// 发现
    public static final int TOPIC = 1;// 主题
    public static final int MSG = 2;// 消息
    public static final int PERSONAL = 3;// 我的

    @BindView(R.id.vp_main_content)
    NoScrollViewPager mViewPager;

    @BindView(R.id.bottom_nav_add)
    ImageView mIvAdd;

    @BindViews({R.id.bottom_nav_find, R.id.bottom_nav_topic, R.id.bottom_nav_msg, R.id.bottom_nav_personal})
    List<ImageView> imgViews;

    private int[] selectedDrawables = {
            R.drawable.ic_find_selected_24dp,
            R.drawable.ic_topic_selected_24dp,
            R.drawable.ic_msg_selected_24dp,
            R.drawable.ic_personal_selected_24dp};
    private int[] unSelectedDrawables = {
            R.drawable.ic_find_unselected_24dp,
            R.drawable.ic_topic_unselected_24dp,
            R.drawable.ic_msg_unselected_24dp,
            R.drawable.ic_personal_unselected_24dp};
    private Drawable[] selectedDrawable;
    private Drawable[] unSelectedDrawable;


    @Override

    public int getLayoutId() {
        return R.layout.activity_main2;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initImages();
        initViewPager();

    }

    /**
     * 将用到的Drawable图标缓存下来
     */
    private void initImages() {
        selectedDrawable = new Drawable[]{
                ContextCompat.getDrawable(context, R.drawable.ic_find_selected_24dp),
                ContextCompat.getDrawable(context, R.drawable.ic_topic_selected_24dp),
                ContextCompat.getDrawable(context, R.drawable.ic_msg_selected_24dp),
                ContextCompat.getDrawable(context, R.drawable.ic_personal_selected_24dp)

        };
        unSelectedDrawable = new Drawable[]{
                ContextCompat.getDrawable(context, R.drawable.ic_find_unselected_24dp),
                ContextCompat.getDrawable(context, R.drawable.ic_topic_unselected_24dp),
                ContextCompat.getDrawable(context, R.drawable.ic_msg_unselected_24dp),
                ContextCompat.getDrawable(context, R.drawable.ic_personal_unselected_24dp)
        };
        for (int i = 0; i < imgViews.size(); i++) {
            imgViews.get(i).setBackground(selectedDrawable[i]);
            imgViews.get(i).setBackground(unSelectedDrawable[i]);
        }
    }


    private void initViewPager() {
        mViewPager.setScroll(false);
//        mViewPager.setOffscreenPageLimit(4);
        Fragment[] fragments = new Fragment[]{FindFragment.newInstance(), TopicFragment.newInstance(), MsgFragment.newInstance(), PersonalInfoFragment.newInstance()};
        String[] titles = {"发现", "专题", "消息", "我的"};
        mViewPager.setAdapter(new MyViewpagerAdapter(getSupportFragmentManager(), Arrays.asList(fragments)));
        setMenuItem(FIND);
    }

    @Override
    public void setListener() {
        imgViews.get(0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 设置相应ViewPager页
                setMenuItem(FIND);
            }
        });
        imgViews.get(1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setMenuItem(TOPIC);
            }
        });
        imgViews.get(2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkUserlogin()) {
                    setMenuItem(MSG);
                }
            }
        });
        imgViews.get(3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkUserlogin()) {
                    setMenuItem(PERSONAL);
                }
            }
        });
        mIvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 物品发布页面
                Intent intent = new Intent(MainActivity2.this, PostGoodActivity.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 判断用户是否存在，不存在则提示用户去登录
     * @return
     */
    private boolean checkUserlogin() {
        SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
        //判断是否存在昵称，存在则说明用户登录
        if ("".equals(sp.getString("nc", ""))) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage(R.string.user_no_login)
                    .setPositiveButton(R.string.user_go_login, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startActivity(new Intent(MainActivity2.this, LoginActivity.class));
                        }
                    }).show();
            return false;
        }
        return true;
    }

    /**
     * 某一item被选中的操作
     *
     * @param pageIndex
     */
    private void setMenuItem(int pageIndex) {
        // 设置相应ViewPager页
        mViewPager.setCurrentItem(pageIndex);
        // 设置选中图标，其余的不选中图标
        setMenuItemIcon(pageIndex);
    }

    // 设置图标
    private void setMenuItemIcon(int pageIndex) {
        for (int i = 0; i < 4; i++) {
            if (pageIndex == i) {
                // 当前被选中项
                imgViews.get(i).setBackground(selectedDrawable[i]);
            } else {
                // 未被选中项
                imgViews.get(i).setBackground(unSelectedDrawable[i]);
            }
        }
    }

}
